package com.xyd.Servlet;

import com.xyd.Service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/InsertUserServlet")
public class InsertUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService=new UserService();
        //注册添加用户
        String uname = request.getParameter("uname");
        String upass = request.getParameter("upass");
        //输入的验证码
        String veryCode = request.getParameter("veryCode");
        //服务器生成的验证码
        String captchaVeryCode = (String) request.getSession().getAttribute("captcha");
        if (veryCode.equalsIgnoreCase(captchaVeryCode)){
            userService.insertUser(uname,upass);
            request.getSession().setAttribute("uname",uname);
            request.getRequestDispatcher("/reg-result.jsp").forward(request,response);
        }else {
            response.sendRedirect("/register.html");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
